//
//  YSLastResultView.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-10-13.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSLastResultView.h"
#import "YSDieResultView.h"
#import "YSUi.h"
#import "YSType.h"
#import "UIViewExt.h"


static NSInteger diceViewSpace = 6;
static NSInteger diceImageSize = 22;

@implementation YSLastResultView

- (id)initWithFrame:(CGRect)frame contentInfo:(NSDictionary *)content
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        bg.image = [UIImage imageNamed:@"result_bg"];
        [self addSubview:bg];
        
        NSInteger nTopSpace = 10;
        NSInteger nLeftSpace = 4;
        
        NSArray *diceArray = content[@"diceList"];
        NSInteger nIndex = 0;
        for (NSDictionary *dict in diceArray) {
            UIView *subView = [self createDiceView:dict];
            subView.frame = CGRectMake(nLeftSpace, nIndex * (diceImageSize + diceViewSpace) + nTopSpace, 140, diceImageSize);
            [self addSubview:subView];
            
            nIndex ++;
        }
        
        UILabel *label1 = [[UILabel alloc] init];
        label1.backgroundColor = [UIColor clearColor];
        label1.textColor = [UIColor yellowColor];
        label1.font = [UIFont systemFontOfSize:14];
        label1.textAlignment = NSTextAlignmentCenter;
        
        UIButton *continueBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [continueBtn setBackgroundImage:[UIImage imageNamed:@"ready_btn_n"]
                               forState:UIControlStateNormal];
        [continueBtn setBackgroundImage:[UIImage imageNamed:@"ready_btn_h"]
                               forState:UIControlStateHighlighted];
        continueBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [continueBtn addTarget:self
                        action:@selector(continueBtnClick)
              forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *label2 = [[UILabel alloc] init];
        label2.textColor = [UIColor whiteColor];
        label2.backgroundColor = [UIColor clearColor];
        label2.font = [UIFont systemFontOfSize:14];
        label2.textAlignment = NSTextAlignmentCenter;
        
        UIImageView *faceImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width / 2, 10, 87, 87)];
        NSInteger resultType = [content[@"win"] integerValue];
        NSString *score = content[@"score"];
        
        
        if (resultType == eResultWin) {
            faceImageView.image = [UIImage imageNamed:@"win_face"];
            
            label1.text = @"你赢啦!";
            label2.text = [NSString stringWithFormat:@"恭喜你获得%@幸运豆:",score];
            [continueBtn setTitle:@"乘胜追击" forState:UIControlStateNormal];
            
        } else {
            faceImageView.image = [UIImage imageNamed:@"lose_face"];
            
            label1.text = @"你输啦!";
            label2.text = [NSString stringWithFormat:@"不小心丢失%@幸运豆:",score];
            [continueBtn setTitle:@"再接再厉" forState:UIControlStateNormal];
        }
        [self addSubview:faceImageView];
        
        label1.frame = CGRectMake(faceImageView.right + 10, faceImageView.top + 16, 70, 16);
        continueBtn.frame = CGRectMake(faceImageView.right + 10, label1.bottom + 6, 70, 26);
        
        label2.frame = CGRectMake(self.frame.size.width / 2, faceImageView.bottom + 10, self.frame.size.width / 2, 20);
        
        [self addSubview:label1];
        [self addSubview:continueBtn];
        [self addSubview:label2];
        
        UIImageView *resultBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shai_view_bg"]];
        resultBg.frame = CGRectMake(10, self.height - 10 - 40, self.width / 2 - 20, 40);
        
        UILabel *resultLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(18, 0, 60, 40)];
        resultLabel1.backgroundColor = [UIColor clearColor];
        resultLabel1.font = [UIFont systemFontOfSize:12];
        resultLabel1.text = @"开罐结果";
        resultLabel1.textColor = [UIColor whiteColor];
        [resultBg addSubview:resultLabel1];
        
        /*
         diceNum = 0;
         dicePoint = 3;
         */
        UILabel *resultLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(resultLabel1.right + 4, 0, 30, 40)];
        resultLabel2.backgroundColor = [UIColor clearColor];
        resultLabel2.font = [UIFont systemFontOfSize:15];
        resultLabel2.text = [NSString stringWithFormat:@"%d X ",[content[@"diceNum"] intValue]];
        resultLabel2.textColor = [UIColor yellowColor];
        [resultBg addSubview:resultLabel2];
        
        NSString *imageName = [NSString stringWithFormat:@"bs_%d",[content[@"dicePoint"] intValue]];
        UIImageView *resultImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        resultImage.frame = CGRectMake(resultLabel2.right + 6, 8, 24, 24);
        [resultBg addSubview:resultImage];
        
        [self addSubview:resultBg];
        
        
        UIImageView *shareBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shai_view_bg"]];
        shareBg.frame = CGRectMake(resultBg.right + 20, resultBg.top, self.width / 2 - 20, 40);
        
        UILabel *shareLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 60, 40)];
        shareLabel.text = @"分享到";
        shareLabel.textColor = [UIColor whiteColor];
        shareLabel.backgroundColor = [UIColor clearColor];
        shareLabel.font = [UIFont systemFontOfSize:14];
        [shareBg addSubview:shareLabel];
        
        [self addSubview:shareBg];
        
        UIButton *weiBoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [weiBoBtn setImage:[UIImage imageNamed:@"share_sina"] forState:UIControlStateNormal];
        weiBoBtn.frame = CGRectMake(resultBg.right + 20 + 70, shareBg.top + 4, 34, 34);
        [weiBoBtn addTarget:self action:@selector(shareWeiBo) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:weiBoBtn];
        
        UIButton *weiXinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [weiXinBtn setImage:[UIImage imageNamed:@"share_wx"] forState:UIControlStateNormal];
        weiXinBtn.frame = CGRectMake(weiBoBtn.right + 8, shareBg.top + 4, 34, 34);
        [weiXinBtn addTarget:self action:@selector(shareWeiXin) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:weiXinBtn];
    }
    return self;
}


- (UIView *)createDiceView:(NSDictionary *)info
{
    NSInteger diceSpace = 4;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, diceImageSize)];
    view.backgroundColor = [UIColor clearColor];
    
    
    NSString *userName = info[@"userName"];
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 0, 36, diceImageSize)];
    nameLabel.font = [UIFont systemFontOfSize:10];
    nameLabel.text = userName;
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.backgroundColor = [UIColor clearColor];
    [view addSubview:nameLabel];
    
    
    NSString *diceStr = info[@"dice"];
    NSArray *array = [diceStr componentsSeparatedByString:@"-"];
    
    int nIndex = 0;
    for (NSString *numStr in array) {
        NSString *imageName = [NSString stringWithFormat:@"bs_%@",numStr];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(nameLabel.right + (nIndex * (diceSpace + diceImageSize)), 0, diceImageSize, diceImageSize);
        [view addSubview:imageView];
        
        nIndex ++;
    }
    
    return view;
}

- (void)continueBtnClick {
    if (_delegate && [_delegate respondsToSelector:@selector(lastResultClose)]) {
        [_delegate lastResultClose];
    }
}

- (void)shareWeiBo {
    if (_delegate && [_delegate respondsToSelector:@selector(lastResultShare:)]) {
        [_delegate lastResultShare:eShareWeiBo];
    }
}

- (void)shareWeiXin {
    if (_delegate && [_delegate respondsToSelector:@selector(lastResultShare:)]) {
        [_delegate lastResultShare:eShareWeiXin];
    }
}

@end
